import React from 'react';
import {Button, Card, Form, Input, Radio} from "antd";
import {Link, useLocation, useNavigate} from "react-router-dom";
import {GatewayRoute, GatewayRouteFactory} from "../../../type/entity/GatewayRoute";
import {InputMultiple} from "../../../common/form";
import TextArea from "antd/es/input/TextArea";
import {filedArrayToStringObject} from "../../../util/compare";
import RequestHandler from "../../../http/RequestHandler";

const RouterFactoryAdd = () => {

    const router = useLocation().state as GatewayRoute;

    const navigate = useNavigate();


    const onFinish = async (value: GatewayRouteFactory) => {
        value.args = filedArrayToStringObject(value.args as any);
        value.metadata = filedArrayToStringObject(value.metadata as any);
        const res = await RequestHandler.put<number>("/manager/admin/route/factory", value);
        // if (res.success && res.data > 0) {
        //     message.success("提交修改成功");
        //     setTimeout(() => navigate("../", {state: router}), 500);
        // }else {
        //     message.info("修改失败");
        // }
    };

    return (
        <Card title={"添加工厂信息"} extra={<Link to={".."}>工厂列表</Link>}>
            <Form<GatewayRouteFactory>
                name="dynamic_form_nest_item"
                onFinish={onFinish}
                labelCol={{span: 4}}
                wrapperCol={{span: 16}}
                autoComplete="off"
                initialValues={{type: "predicate", routeId: router.id}}
            >
                <Form.Item<GatewayRouteFactory> name={"routeId"} label={"数据编号"} hidden>
                    <Input/>
                </Form.Item>
                <Form.Item<GatewayRouteFactory> name={"name"} label={"工厂名字"}>
                    <Input/>
                </Form.Item>
                <Form.Item<GatewayRouteFactory> name={"type"} label={"工厂类型"}>
                    <Radio.Group>
                        <Radio value="predicate"> 断言工厂 </Radio>
                        <Radio value="filter"> 过滤器工厂 </Radio>
                    </Radio.Group>
                </Form.Item>
                <InputMultiple<GatewayRouteFactory> name={"args"} label={"工厂参数"}/>
                <InputMultiple<GatewayRouteFactory> name={"metadata"} label={"工厂元数据"}/>
                <Form.Item<GatewayRouteFactory> name={"remark"} label={"备注信息"}>
                    <TextArea rows={3}/>
                </Form.Item>
                <Form.Item wrapperCol={{span: 6, offset: 4}}>
                    <Button type="primary" htmlType="submit">
                        提交修改
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    );
};

export default RouterFactoryAdd;